localPath="C:/Users/pumcl/.Neo4jDesktop/neo4jDatabases/database-22fa26f3-fb1e-4053-a039-7cdd08a9956c/installation-3.4.9/data/databases/GRAPH.db"
localTempPath="C:/Users/pumcl/.Neo4jDesktop/neo4jDatabases/database-22fa26f3-fb1e-4053-a039-7cdd08a9956c/installation-3.4.9/data/databases/temp"
neo4jAdminTool="C:/Users/pumcl/.Neo4jDesktop/neo4jDatabases/database-22fa26f3-fb1e-4053-a039-7cdd08a9956c/installation-3.4.9/bin/neo4j-admin.bat"

remoteUser="root"
remoteHost="192.168.1.103"
remoteDbPath="/var/lib/docker/volumes/aabdbdd1ee8660a972aea3a65b60b7a7c0a1d048ff4e591a8b27766594fa3664/_data/databases/graph.db"
remoteDbBackupPath="/root/Olivia/backup"


# localPath="/c/Users/velen/.Neo4jDesktop/neo4jDatabases/database-17e558af-a5ad-499d-b933-126a64246a56/installation-3.5.2/data/databases/graph.db"
# localTempPath="/c/Users/velen/.Neo4jDesktop/neo4jDatabases/database-17e558af-a5ad-499d-b933-126a64246a56/installation-3.5.2/data/databases/temp"
# neo4jAdminTool="/c/Users/velen/.Neo4jDesktop/neo4jDatabases/database-17e558af-a5ad-499d-b933-126a64246a56/installation-3.5.2/bin/neo4j-admin.bat"

# remoteUser="root"
# remoteHost="118.89.222.14"
# remoteDbPath="/var/lib/docker/volumes/962073207efb44f278708c7e1fa7f9e907753ffb2a031758e5ab85f22357b706/_data/databases/graph.db"
# remoteDbBackupPath="/root/Olivia/backup"


remotePrefix=${remoteUser}@${remoteHost}
remoteDbPathWithPrefix=${remotePrefix}:${remoteDbPath}
remoteDbBackupPathWithPrefix=${remotePrefix}:${remoteDbBackupPath}

currDate=`date '+%Y%m%d%H%M%S'`

function pressToExit()
{
    echo ""
    read -p "Press any key to exit. " exitcmd
    exit
}

echo "[1] 本地（最新）=> 服务器"

echo "[2] 本地 <= 服务器（最新）"

echo "[3] 本地 <= 服务器（手动）"

echo "[4] 服务器数据库备份"

echo "[5] 本地云备份"

echo ""
read -p "请选择需执行命令的序号：" cmdnum
echo ""


# 本地（最新版本）更新至服务器
if [ ${cmdnum} = "1" ]; 
then
    echo ""
    read -p "热备份需要将本地 Neo4j 开启，请确认[Y/N]: " cnf
    echo ""

    if [ ${cnf} = "Y" ]; 
    then
        rm -rf ${localTempPath}/graph.db_${currDate}
        ${neo4jAdminTool} backup --backup-dir=${localTempPath} --name=graph.db_${currDate}
        scp -r ${localTempPath}/graph.db_${currDate} ${remoteDbBackupPathWithPrefix}/graph.db_${currDate}
        ssh ${remotePrefix} "rm -rf ${remoteDbPath}"
        ssh ${remotePrefix} "cp -rf ${remoteDbBackupPath}/graph.db_${currDate} ${remoteDbPath}"
        ssh ${remotePrefix} "docker restart neo4j"

        if [ $? != "0" ];
        then
            echo "Error. Exit. 1"
            pressToExit
        else
            echo ""
            echo "Server database upgrade successfully."
        fi
    else
        echo ""
        pressToExit
    fi


# 服务器（最新版本）更新至本地
elif [ ${cmdnum} = "2" ];
then
    echo ""
    read -p "数据恢复需要将本地 Neo4j 服务关闭，请确认[Y/N]: " cnf
    echo ""
    if [ ${cnf} = "Y" ]; 
    then
        ssh ${remotePrefix} "docker stop neo4j"
        rm -rf ${localTempPath}/graph.db_${currDate}
        ssh ${remotePrefix} "cp -rf ${remoteDbPath} ${remoteDbBackupPath}/graph.db_${currDate}"
        scp -r ${remoteDbBackupPathWithPrefix}/graph.db_${currDate} ${localTempPath}/graph.db_${currDate}
        ssh ${remotePrefix} "docker start neo4j"
        ${neo4jAdminTool} restore --from=${localTempPath}/graph.db_${currDate} --database=graph.db --force=true

        if [ $? != "0" ];
        then
            echo "Error. Exit. 2" 
            pressToExit
        else
            echo ""
            echo "Local database upgrade successfully. Please restart local Neo4j service manually."
            echo ""
        fi
    else
        echo ""
        pressToExit
    fi


# 服务器（手动选择）更新至本地
elif [ ${cmdnum} = "3" ];
then
    echo ""
    read -p "数据恢复需要将本地 Neo4j 服务关闭，请确认[Y/N]: " cnf
    echo ""
    if [ ${cnf} = "Y" ]; 
    then
        ssh ${remotePrefix} "ls -l ${remoteDbBackupPath}"
        echo ""
        read -t 100 -p "Please choose one graph.db name (Eg. graph.db_20190214230722): " DbName
        echo ""

        rm -rf ${localTempPath}/graph.db
        scp -r ${remoteDbBackupPathWithPrefix}/${DbName} ${localTempPath}/graph.db_${currDate}
        ${neo4jAdminTool} restore --from=${localTempPath}/graph.db_${currDate} --database=graph.db --force=true

        if [ $? != "0" ];
        then
            echo "Error. Exit. 3" 
            pressToExit
        else
            echo "" 
            echo "Local database upgrade successfully. Please restart local Neo4j service manually."
        fi
    else
        echo "" 
        pressToExit
    fi


# 服务器手动备份
elif [ ${cmdnum} = "4" ];
then
    ssh ${remotePrefix} "docker stop neo4j"
    ssh ${remotePrefix} "cp -rf ${remoteDbPath} ${remoteDbBackupPath}/graph.db_${currDate}"
    ssh ${remotePrefix} "docker start neo4j"

    if [ $? != "0" ];
    then
        echo "Error. Exit. 4"
        pressToExit
    else
        echo "" 
        echo "Server database Backup successfully. Currently you've get following backups:" 
        echo ""
        ssh ${remotePrefix} "ls -l ${remoteDbBackupPath}"
        echo ""
    fi


# 本地大版本推上云
elif [ ${cmdnum} = "5" ];
then
    git add ./graph.db/
    read -p "Please input the commit message: " commitMsg
    git commit -m "${commitMsg}"
    git push origin master
fi


pressToExit